package com.ruoyi.project.system.coinrecord.service.impl;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.project.system.coinrecord.dto.HaoqiCoinRecordDto;
import com.ruoyi.project.system.coinrecord.mapper.HaoqiCoinRecordMapper;
import com.ruoyi.project.system.coinrecord.domain.HaoqiCoinRecord;
import com.ruoyi.project.system.coinrecord.service.IHaoqiCoinRecordService;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import com.ruoyi.project.system.shopconfig.domain.ShopConfig;
import com.ruoyi.project.system.shopconfig.mapper.ShopConfigMapper;

/**
 * 好奇币记录Service业务层处理
 * 
 * @author f_baisi
 * @date 2020-06-29
 */
@Service
public class HaoqiCoinRecordServiceImpl implements IHaoqiCoinRecordService 
{
    @Resource
    private IdGererateFactory idGererateFactory;
    @Autowired
    private HaoqiCoinRecordMapper haoqiCoinRecordMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private ShopConfigMapper shopConfigMapper;

    /**
     * 查询好奇币记录
     * 
     * @param id 好奇币记录ID
     * @return 好奇币记录
     */
    @Override
    public HaoqiCoinRecord selectHaoqiCoinRecordById(String id)
    {
        return haoqiCoinRecordMapper.selectHaoqiCoinRecordById(id);
    }

    /**
     * 查询好奇币记录列表
     * 
     * @param haoqiCoinRecord 好奇币记录
     * @return 好奇币记录
     */
    @Override
    public List<HaoqiCoinRecordDto> selectHaoqiCoinRecordList(HaoqiCoinRecord haoqiCoinRecord)
    {
        return haoqiCoinRecordMapper.selectHaoqiCoinRecordList(haoqiCoinRecord);
    }

    /**
     * 新增好奇币记录
     * 
     * @param haoqiCoinRecord 好奇币记录
     * @return 结果
     */
    @Override
    public int insertHaoqiCoinRecord(HaoqiCoinRecord haoqiCoinRecord)
    {
        haoqiCoinRecord.setId(this.idGererateFactory.nextStringId());
        return haoqiCoinRecordMapper.insertHaoqiCoinRecord(haoqiCoinRecord);
    }

    /**
     * 修改好奇币记录
     * 
     * @param haoqiCoinRecord 好奇币记录
     * @return 结果
     */
    @Override
    public int updateHaoqiCoinRecord(HaoqiCoinRecord haoqiCoinRecord)
    {
        return haoqiCoinRecordMapper.updateHaoqiCoinRecord(haoqiCoinRecord);
    }

    /**
     * 删除好奇币记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHaoqiCoinRecordByIds(String ids)
    {
        return haoqiCoinRecordMapper.deleteHaoqiCoinRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除好奇币记录信息
     * 
     * @param id 好奇币记录ID
     * @return 结果
     */
    @Override
    public int deleteHaoqiCoinRecordById(String id)
    {
        return haoqiCoinRecordMapper.deleteHaoqiCoinRecordById(id);
    }

    @Override
    public void returnPrice(String memberId, BigDecimal price,String resId,Integer type) {
        //查询当前用户
        Member member = this.memberMapper.selectMemberById(memberId);
        //查询返费比例
        ShopConfig shopConfig = this.shopConfigMapper.findAll().get(0);
        //用户应得金额
        BigDecimal one = price.multiply(shopConfig.getOneReturn()).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        //添加记录
        HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
        haoqiCoinRecord.setResId(resId);
        haoqiCoinRecord.setType(0);
        haoqiCoinRecord.setGetType(type);
        haoqiCoinRecord.setNum(one);
        haoqiCoinRecord.setMemberId(memberId);
        haoqiCoinRecord.setCreated(DateUtils.getNowDate());
        haoqiCoinRecord.setStatus(1);
        //更新用户余额
        Member update = new Member();
        if(member.getIsVip()==1){
            this.insertHaoqiCoinRecord(haoqiCoinRecord);
            update.setId(memberId);
            update.setHaoqiCoin(member.getHaoqiCoin().add(one));
            this.memberMapper.updateMember(update);
        }
        //判断是否存在上级
        if(member.getIntroUser()!=null){
            Member twoMember = this.memberMapper.selectMemberById(member.getIntroUser());
            //如果邀请人是加盟商 则返利
            if(twoMember.getIsVip()==1){
                //用户应得金额
                BigDecimal two = price.multiply(shopConfig.getTwoReturn()).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                //添加记录
                haoqiCoinRecord.setResId(resId);
                haoqiCoinRecord.setNum(two);
                haoqiCoinRecord.setMemberId(twoMember.getId());
                this.insertHaoqiCoinRecord(haoqiCoinRecord);

                //更新用户余额
                update.setId(twoMember.getId());
                update.setHaoqiCoin(twoMember.getHaoqiCoin().add(two));
                this.memberMapper.updateMember(update);
            }
            //判断是否存在上级
            if(twoMember.getIntroUser()!=null){

                Member threeMember = this.memberMapper.selectMemberById(twoMember.getIntroUser());
                //如果邀请人是加盟商 则返利
                if(threeMember.getIsVip()==1){
                    //用户应得金额
                    BigDecimal three = price.multiply(shopConfig.getThreeReturn()).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                    //添加记录
                    haoqiCoinRecord.setResId(resId);
                    haoqiCoinRecord.setNum(three);
                    haoqiCoinRecord.setMemberId(threeMember.getId());
                    this.insertHaoqiCoinRecord(haoqiCoinRecord);

                    //更新用户余额
                    update.setId(threeMember.getId());
                    update.setHaoqiCoin(threeMember.getHaoqiCoin().add(three));
                    this.memberMapper.updateMember(update);
                }
            }
        }
    }
}
